package com.example.audio_test.ui

import android.graphics.ImageFormat
import android.hardware.Camera
import android.media.AudioFormat
import android.media.MediaRecorder
import android.os.Environment
import android.view.SurfaceHolder
import com.alibaba.android.arouter.facade.annotation.Route
import com.example.audio_test.R
import com.example.audio_test.bean.EncoderParams
import com.example.audio_test.utils.AACEncodeConsumer
import com.example.audio_test.utils.H264EncoderConsumer
import com.example.audio_test.utils.MediaMuxerUtil
import com.kun.base.ui.AbsBaseActivity
import kotlinx.android.synthetic.main.activity_video_record.*
import java.io.File
import java.io.IOException

/**
 *视频音频录制并合成mp4 格式
 *
 * @author : Kun
 * @date : 2021/6/28
 * @time : 17:08
 */
@Route(path = "/recordTest/VideoRecordActivity")
class VideoRecordActivity : AbsBaseActivity(), SurfaceHolder.Callback, Camera.PreviewCallback {

    private var camera: Camera? = null

    private var h264EncoderConsumer: H264EncoderConsumer? = null
    private var aacEncoderConsumer: AACEncodeConsumer? = null
    private var mediaMuxerUtil: MediaMuxerUtil? = null

    private var filePath = ""
    private var params: EncoderParams? = null

    override fun getLayoutId(): Int {
        return R.layout.activity_video_record
    }

    override fun initViewModel() {
    }

    override fun initView() {
        btn_start.setOnClickListener {
            //开始
            startRecord()
        }
        btn_end.setOnClickListener {
            //结束
            endRecord()
        }

        surface_view.holder.addCallback(this)
        val file = File(getExternalFilesDir(Environment.DIRECTORY_MOVIES), "test.mp4")
        if (file.exists()) {
            file.delete()
        }
        filePath = file.absolutePath

        mediaMuxerUtil = MediaMuxerUtil(filePath, 20000)
        aacEncoderConsumer = AACEncodeConsumer()
        h264EncoderConsumer = H264EncoderConsumer()
        params = getEncodeParams()
        aacEncoderConsumer!!.setTmpuMuxer(mediaMuxerUtil!!, params!!)
        h264EncoderConsumer!!.setTmpuMuxer(mediaMuxerUtil!!, params!!)
    }

    override fun surfaceCreated(holder: SurfaceHolder) {
        camera = Camera.open()
        camera!!.setDisplayOrientation(90)
        val parameters = camera!!.parameters
        parameters.previewFormat = ImageFormat.NV21
        parameters.setPreviewSize(1280, 720)
        try {
            camera!!.parameters = parameters
            camera!!.setPreviewDisplay(holder)
            camera!!.setPreviewCallback(this)
            camera!!.startPreview()
        } catch (e: IOException) {
            e.printStackTrace()
        }
    }

    override fun surfaceChanged(holder: SurfaceHolder, format: Int, width: Int, height: Int) {

    }

    override fun surfaceDestroyed(holder: SurfaceHolder) {
        if (camera != null) {
            camera!!.setPreviewCallback(null)
            camera!!.stopPreview()
            camera = null
        }
    }

    override fun onPreviewFrame(data: ByteArray?, camera: Camera?) {
        h264EncoderConsumer!!.addData(data!!)
    }

    private fun startRecord() {
        aacEncoderConsumer!!.start()
        h264EncoderConsumer!!.start()
    }

    private fun endRecord() {
        aacEncoderConsumer!!.exit()
        h264EncoderConsumer!!.exit()
    }

    private fun getEncodeParams(): EncoderParams {

        return EncoderParams(
            filePath,
            1280,
            720,
            H264EncoderConsumer.Quality.MIDDLE,
            H264EncoderConsumer.FrameRate.FPS_25,
            false,
            "" +
                    "",
            16000,
            1,
            8000,
            AudioFormat.CHANNEL_IN_MONO,
            AudioFormat.ENCODING_PCM_16BIT,
            MediaRecorder.AudioSource.MIC
        )
    }
}